package com.xltq.conformity.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xltq.conformity.dao.AdminUserDao;
import com.xltq.conformity.entity.AdminUser;
import com.xltq.conformity.service.AdminUserService;
import com.xltq.conformity.vo.PageVo;
import com.xltq.conformity.vo.TableVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Service
public class AdminUserServiceImpl implements AdminUserService {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private AdminUserDao adminUserDao;

    @Override
    public AdminUser selectByOne(String username) {
        AdminUser adminUser = new AdminUser();
        adminUser.setRealName(username);
        AdminUser selectOne = adminUserDao.selectOne(adminUser);
        if (Objects.isNull(selectOne)) {
            return null;
        } else {
            return selectOne;
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PageInfo<AdminUser> queryAll(PageVo<AdminUser> pageVo, AdminUser adminUser) {
        PageHelper.startPage(pageVo.getPage(), pageVo.getLimit());
        List<AdminUser> allUsers = adminUserDao.getAllUsers(adminUser);
        PageInfo<AdminUser> pageInfo = new PageInfo<>(allUsers);
        return pageInfo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean del(Integer[] ids) {
        Integer del = adminUserDao.del(ids);
        return del > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean add(AdminUser adminUser) {
        if (Objects.isNull(adminUser)) {
            return false;
        }
        int insert = adminUserDao.insert(adminUser);
        return insert > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean update(AdminUser adminUser) {
        if (Objects.isNull(adminUser)) {
            return false;
        }
        int i = adminUserDao.updateByPrimaryKey(adminUser);
        return i > 0;
    }


}
